package jvm.t1;

/**
 * Remarks:局部变量的线程安全问题
 * Class name :Demo1_17
 * Date :2023/2/8 10:43
 * Author:杨俊源
 */
public class Demo1_17 {

    public static void main(String[] args) { ;
        StringBuilder sb = new StringBuilder();
        sb.append(4);
        sb.append(5);
        sb.append(6);
        new Thread(()->{
           m2(sb);
        }).start();
    }

    /**
     * 线程安全
     */
    public static void m1() {
        StringBuilder sb = new StringBuilder();
        sb.append(1);
        sb.append(2);
        sb.append(3);
        System.out.println(sb.toString());
    }

    /**
     * 线程不安全，可能又多个线程共同访问
     * @param sb
     */
    public static void m2(StringBuilder sb) {
        sb.append(1);
        sb.append(2);
        sb.append(3);
        System.out.println(sb.toString());
    }

    /**
     * 线程安全
     * @return
     */
    public static StringBuilder m3() {
        StringBuilder sb = new StringBuilder();
        sb.append(1);
        sb.append(2);
        sb.append(3);
        return sb;
    }
}
